﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.Product;
// ReSharper disable All
namespace ZBJF.Warranty.BLL
{
    public partial class ProductTypeService
    {
        public ProductType LoadByID(long id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }

        public ProductType LoadByName(string productName)
        {
            return LoadEntities(c => c.ProductName == productName).FirstOrDefault();
        }
        public bool IsExist(string ProductName, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ProductType>();
            if (!string.IsNullOrWhiteSpace(ProductName))
                expressionWhere = expressionWhere.And(o => o.ProductName.Equals(ProductName));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }


        /// <summary>
        /// 获得所有配件类型的型号
        /// </summary>
        /// <param name="productTypeID">获得该品牌下所有的配件类型的型号</param>
        /// <returns></returns>
        public IQueryable<ProductType> GetProductType(long productTypeID = -1)
        {
            var expressionWhere = PredicateExtensions.True<ProductType>();
            if (productTypeID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID == productTypeID);
            }
            expressionWhere = expressionWhere.And(o => o.IsAvailable);

            return LoadEntities(expressionWhere).OrderBy(o => o.Sort);
        }


        public IQueryable<ProductType> GetPagingList(ProductTypeModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ProductType>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => o.ProductName.ToLower().Contains(kw) || o.ProductName.ToLower().Contains(kw));
            }
            if (model.IsAvailable.HasValue)
            {
                expressionWhere = expressionWhere.And(c => c.IsAvailable == model.IsAvailable);
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "Sort asc";
            }
            return LoadPagerEntities<ProductType>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}
